package servlets;
/*
import webmanager.model.User;
import webmanager.util.PlanManager;
import webmanager.util.UserManager;
import webmanager.util.stats.PanelContentProvider;
import webmanager.util.stats.StatsManager;*/

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;

import java.io.IOException;


@SuppressWarnings("serial")
public abstract class BaseController extends BaseServlet {
    public static final String ACTION_ID = "actionId";
    public static final int ACTION_DEFAULT = 0;
    private String urlToForward;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String actionStr = request.getParameter(ACTION_ID);
        int actionId = 0;

        try {
            actionId = actionStr != null ? Integer.parseInt(actionStr) : ACTION_DEFAULT;
            urlToForward = handleAction(actionId, request, response);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error en BaseController " + e.toString());//TODO para debuguear exceptions
            e.printStackTrace();
            urlToForward = "../jsp/error404.jsp";
        }

        if (urlToForward != null) {
            forward(request, response, urlToForward);
        }

    }

    /**
     * Handles the controller action and returns the view URL
     *
     * @param actionId
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected abstract String handleAction(int actionId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception;

    protected String getUrlToForward() {
        return urlToForward;
    }

    protected void setUrlToForward(String urlToForward) {
        this.urlToForward = urlToForward;
    }
}
